<template>
  <a-form :model="formState" ref="formDataRef" name="basic" :label-col="{ style: 'width:90px' }" :wrapper-col="{ style: 'calc(100% - 90px)' }" autocomplete="off" @finish="onFinish">
    <a-form-item label="客户" name="customerList" :rules="[{ required: true, message: '请选择客户!' }]">
      <a-form-item-rest>
        <ChooseCustomerAdjust v-model:value="formState.customerList"></ChooseCustomerAdjust>
      </a-form-item-rest>
    </a-form-item>
    <a-form-item label="调价类型" name="adjustmentType" :rules="[{ required: false, message: '请选择调价类型!' }]">
      <a-select v-model:value="formState.adjustmentType" placeholder="调价类型" allowClear>
        <a-select-option v-for="(item, index) in dataDict['QUOTATION_ADJUSTMENT_TYPE']" :key="index" :value="item.code">{{ item.name }}</a-select-option>
      </a-select>
    </a-form-item>
    <template v-if="formState.adjustmentType">
      <a-form-item label="条件" name="conditionDesc" :rules="[{ required: true, message: '请选择条件!' }]">
        <a-select v-model:value="formState.conditionDesc" placeholder="条件">
          <a-select-option v-for="(item, index) in dataDict['QUOTATION_CONDITION']" :key="index" :value="item.code">{{ item.name }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-row v-if="formState.conditionDesc === 'BT'" :gutter="16">
        <a-col :span="14">
          <a-form-item label="数值" name="minValue" :rules="[{ required: true, message: '请填写最小值!' }]">
            <a-input-number v-model:value="formState.minValue" :max="99999" placeholder="最小值" />
          </a-form-item>
        </a-col>
        <a-col :span="10">
          <a-form-item label="" name="maxValue" :rules="[{ required: true, message: '请填写最大值!' }]">
            <a-input-number v-model:value="formState.maxValue" :max="99999" placeholder="最大值" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item v-else label="数值" name="value" :rules="[{ required: true, message: '请填写数值!' }]">
        <a-input-number v-model:value="formState.value" :max="99999" placeholder="数值" />
      </a-form-item>
    </template>
    <a-form-item label="单位" name="unit" :rules="[{ required: true, message: '请选择单位!' }]">
      <a-select v-model:value="formState.unit" placeholder="单位" @change="changeUnitHandle">
        <a-select-option v-for="(item, index) in dataDict['UNIT']" :key="index" :value="item.code">{{ item.name }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="调整值" name="price" :rules="[{ required: true, message: '请填写调整值!' }]">
      <a-input-number v-model:value="formState.price" :max="99999" placeholder="请输入" />
    </a-form-item>
    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button type="primary" html-type="submit">确定</a-button>
      <a-button @click="cancelHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import CommonHook from "@/hook/commonHook";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
import ChooseCustomerAdjust from "../chooseCustomer/index.vue";
import { toRefs, watch } from "vue";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: Base.GeneraDataType;
    type: number;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", visible: boolean): void;
  (e: "submit"): void;
}>();
const { visible, type, passData } = toRefs(props);
const { dataDict } = CommonHook();
const { formState, formDataRef } = InitHook();
const { onFinish, resetFields, cancelHandle, changeUnitHandle } = OperationHook({ formDataRef, formState, type, passData, emit });
const init = () => {
  if (type.value === 2) {
    Object.assign(formState, passData.value);
  }
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (!v) {
      resetFields();
      formDataRef.value.clearValidate();
    } else {
      init();
    }
  }
);
init();
</script>
<style lang="stylus" scoped>
:deep(.ant-input-number){
  width: 100%;
}
</style>
